package com.demo.las.controller;

import com.demo.las.dto.ApiResponse;
import com.demo.las.entity.KnowledgeAsset;
import com.demo.las.service.KnowledgeAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/knowledge-assets")
@CrossOrigin(origins = "*")
public class KnowledgeAssetController {

    @Autowired
    private KnowledgeAssetService knowledgeAssetService;

    @GetMapping
    public ApiResponse<List<KnowledgeAsset>> getAllKnowledgeAssets(@RequestParam Long userId) {
        List<KnowledgeAsset> assets = knowledgeAssetService.getKnowledgeAssetsByUser(userId);
        return ApiResponse.success(assets);
    }

    @GetMapping("/{id}")
    public ApiResponse<KnowledgeAsset> getKnowledgeAssetById(@PathVariable Long id) {
        return knowledgeAssetService.getKnowledgeAssetById(id)
                .map(asset -> ApiResponse.success(asset))
                .orElse(ApiResponse.error(404, "知识资产不存在"));
    }

    @GetMapping("/by-subject")
    public ApiResponse<Optional<KnowledgeAsset>> getKnowledgeAssetBySubject(
            @RequestParam Long userId,
            @RequestParam String subject) {
        Optional<KnowledgeAsset> asset = knowledgeAssetService.getKnowledgeAssetByUserAndSubject(userId, subject);
        return ApiResponse.success(asset);
    }

    @PostMapping
    public ApiResponse<KnowledgeAsset> createKnowledgeAsset(@Valid @RequestBody KnowledgeAsset knowledgeAsset) {
        try {
            KnowledgeAsset createdAsset = knowledgeAssetService.createKnowledgeAsset(knowledgeAsset);
            return ApiResponse.success("知识资产创建成功", createdAsset);
        } catch (Exception e) {
            return ApiResponse.error("创建知识资产失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ApiResponse<KnowledgeAsset> updateKnowledgeAsset(@PathVariable Long id, @Valid @RequestBody KnowledgeAsset knowledgeAsset) {
        try {
            knowledgeAsset.setId(id);
            KnowledgeAsset updatedAsset = knowledgeAssetService.updateKnowledgeAsset(knowledgeAsset);
            return ApiResponse.success("知识资产更新成功", updatedAsset);
        } catch (Exception e) {
            return ApiResponse.error("更新知识资产失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteKnowledgeAsset(@PathVariable Long id) {
        try {
            knowledgeAssetService.deleteKnowledgeAsset(id);
            return ApiResponse.success("知识资产删除成功", null);
        } catch (Exception e) {
            return ApiResponse.error("删除知识资产失败: " + e.getMessage());
        }
    }

    @GetMapping("/total-value")
    public ApiResponse<BigDecimal> getTotalAssetValue(@RequestParam Long userId) {
        BigDecimal totalValue = knowledgeAssetService.getTotalAssetValue(userId);
        return ApiResponse.success(totalValue);
    }

    @GetMapping("/current-value")
    public ApiResponse<BigDecimal> getTotalCurrentValue(@RequestParam Long userId) {
        BigDecimal currentValue = knowledgeAssetService.getTotalCurrentValue(userId);
        return ApiResponse.success(currentValue);
    }

    @GetMapping("/average-mastery")
    public ApiResponse<BigDecimal> getAverageMasteryLevel(@RequestParam Long userId) {
        BigDecimal averageMastery = knowledgeAssetService.getAverageMasteryLevel(userId);
        return ApiResponse.success(averageMastery);
    }

    @GetMapping("/mastery-by-subject")
    public ApiResponse<List<Object[]>> getMasteryLevelBySubject(@RequestParam Long userId) {
        List<Object[]> masteryData = knowledgeAssetService.getMasteryLevelBySubject(userId);
        return ApiResponse.success(masteryData);
    }

    @GetMapping("/overview")
    public ApiResponse<List<Object[]>> getAssetOverview(@RequestParam Long userId) {
        List<Object[]> overview = knowledgeAssetService.getAssetOverviewByUser(userId);
        return ApiResponse.success(overview);
    }

    @GetMapping("/recently-studied")
    public ApiResponse<List<KnowledgeAsset>> getRecentlyStudiedAssets(@RequestParam Long userId) {
        List<KnowledgeAsset> recentAssets = knowledgeAssetService.getRecentlyStudiedAssets(userId);
        return ApiResponse.success(recentAssets);
    }

    @GetMapping("/need-review")
    public ApiResponse<List<KnowledgeAsset>> getAssetsNeedReview(@RequestParam Long userId) {
        List<KnowledgeAsset> assetsNeedReview = knowledgeAssetService.getAssetsNeedReview(userId);
        return ApiResponse.success(assetsNeedReview);
    }

    @GetMapping("/count-above-mastery")
    public ApiResponse<Long> countAssetsAboveMasteryLevel(
            @RequestParam Long userId,
            @RequestParam BigDecimal minLevel) {
        Long count = knowledgeAssetService.countAssetsAboveMasteryLevel(userId, minLevel);
        return ApiResponse.success(count);
    }
}




